//
//  SelfCenterViewController.m
//  customer
//
//  Created by Holothurian on 2018/10/19.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "SelfCenterViewController.h"
#import "BaseWebViewViewController.h"
#import "SelfCenterUserHeaderTableCell.h"
#import "SelfInformationViewController.h"
#import "AppointmentCenterViewController.h"
#import "MyOrderListViewController.h"
#import "DecorateOrdersCenterViewController.h"
#import "TicketsRecordCenterViewController.h"
#import "SsehSettingViewController.h"
#import "CouponListViewController.h"

static NSString *SelfCenterCellID = @"SelfCenterCellID";
static NSString *CustomCellID = @"CustomCellID";

@interface SelfCenterViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

@property (nonatomic,strong)MemberInfoManager *infoModel;

@end

@implementation SelfCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"个人中心";
    self.tableView.bounces = NO;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:SelfCenterCellID];
    [self.tableView registerClass:[SelfCenterUserHeaderTableCell class] forCellReuseIdentifier:CustomCellID];
}

- (void)getData{
    NSDictionary *dict = @{@"member_id":[NSString emptyStr:K_UserDefault_Read(CustomerUserID)]?:@"",
                           @"member_token":[NSString emptyStr:K_UserDefault_Read(CustomerUserToken)]?:@""};
    AutoWeakSelf
    [[QYNetManager sharedManager]POST:kGetMemberDetail parameters:dict andApiTyp:CustomerApiTypMain SuccessBlock:^(id responseObject) {
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
            MemberInfoManager *model = [MemberInfoManager yy_modelWithDictionary:responseObject[@"data"]];
            [weakSelf.dataList replaceObjectAtIndex:0 withObject:@[@[model.member_nick_name?:@"用户昵称",model.member_img?:@"selfCenter_HeaderImg",model.member_account?:kContactPhone]]];
            weakSelf.infoModel = model;
            [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
        }
    } Failure:^(NSError *error) {
        //[weakSelf showErrorTipWithTitle:CustomerPoolNetStr];
    }];
}

- (void)loadDataList{
    NSArray *topArr = @[@[@"用户昵称",@"selfCenter_HeaderImg",@"18888888888"]];
    NSArray *centerArr = @[@[@"优惠券",@"selfCenter_Coupon"],@[@"维修订单",@"selfCenter_RepairOrder"],@[@"装修订单",@"selfCenter_DecorateOrder"],@[@"装配订单",@"selfCenter_AddOrder"],@[@"开票记录",@"selfCenter_TicketRecord"],@[@"预约中心",@"selfCenter_Appointment"],@[@"联系我们",@"selfCenterMobile"]];
    NSArray *bottomArr = @[@[@"设置",@"selfCenterSet"]];
    [self.dataList addObject:topArr];
    [self.dataList addObject:centerArr];
    [self.dataList addObject:bottomArr];
    [self.tableView reloadData];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    self.navigationController.navigationBar.barTintColor = [HSPublic colorWithHexString:@"0082DF"];
    self.navigationController.navigationBar.alpha = 1.0;
    [self getData];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor blackColor]};
    self.navigationController.navigationBar.barTintColor = [UIColor appColor];;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   return [self.dataList count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [(NSMutableArray *)self.dataList[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section < self.dataList.count && indexPath.row < [(NSMutableArray *)self.dataList[indexPath.section] count]) {
        if (indexPath.section == 0) {
            SelfCenterUserHeaderTableCell *customCell = [self.tableView dequeueReusableCellWithIdentifier:CustomCellID];
            customCell.nameLabel.text = self.dataList[indexPath.section][indexPath.row][0];
            customCell.phoneLabel.text = self.dataList[indexPath.section][indexPath.row][2];
            if (![self.infoModel.member_img containsString:@"http"]) {
                [customCell.headImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kCustomerImageIP,self.dataList[indexPath.section][indexPath.row][1]]] placeholderImage:[UIImage imageNamed:@"selfCenter_HeaderImg"]];
            }else{
                [customCell.headImgView sd_setImageWithURL:[NSURL URLWithString:self.dataList[indexPath.section][indexPath.row][1]] placeholderImage:[UIImage imageNamed:@"selfCenter_HeaderImg"]];
            }
            
            return customCell;
        }else{
            UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:SelfCenterCellID];
            cell.backgroundColor = [UIColor whiteColor];
            cell.textLabel.text = self.dataList[indexPath.section][indexPath.row][0];
            cell.imageView.image = [UIImage imageNamed:self.dataList[indexPath.section][indexPath.row][1]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            return cell;
        }
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger index = indexPath.section * 10 + indexPath.row;
    switch (index) {
        case 0:{//用户信息
            SelfInformationViewController *infoVC = [SelfInformationViewController new];
            infoVC.nickname = self.dataList[0][0][0];
            infoVC.imgUrl = self.dataList[0][0][1];
            infoVC.phone = self.dataList[0][0][2];
            TAB_PUSH(self, infoVC, YES);
        }break;
        case 10:{//优惠券
            CouponListViewController *vc = [[CouponListViewController alloc]init];
            TAB_PUSH(self, vc, YES);
            return;
        }break;
        case 11:{//维修订单
            TAB_SELECT(1)
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[NSNotificationCenter defaultCenter]postNotificationName:kPostNotificationForRepairOrder object:@{@"postIndex":@"0"} userInfo:nil];
            });
            return;
        }break;
        case 12:{//装修订单
            DecorateOrdersCenterViewController *orderDetail = [[DecorateOrdersCenterViewController alloc]init];
            orderDetail.selectIndex = 0;
            TAB_PUSH(self, orderDetail, YES);
        }break;
        case 13:{//装配订单
            DecorateOrdersCenterViewController *orderDetail = [[DecorateOrdersCenterViewController alloc]init];
            orderDetail.selectIndex = 1;
            TAB_PUSH(self, orderDetail, YES);
        }break;
        case 14:{//开票记录
            TicketsRecordCenterViewController *ticketVC = [[TicketsRecordCenterViewController alloc]init];
            TAB_PUSH(self, ticketVC, YES);
        }break;
        case 15:{//预约中心
            AppointmentCenterViewController *appointCenter = [[AppointmentCenterViewController alloc]init];
            appointCenter.selectIndex = 0;
            TAB_PUSH(self, appointCenter, YES);
        }break;
        case 16:{//联系我们
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:kContactPhone preferredStyle:UIAlertControllerStyleActionSheet];
            [alertController addAction:[UIAlertAction actionWithTitle:@"呼叫" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:kContactPhone]];
            }]];
            [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [alertController dismissViewControllerAnimated:YES completion:nil];
            }]];
            [self presentViewController:alertController animated:YES completion:nil];
            return;
        }break;
        case 20:{//设置
            SsehSettingViewController * settingVC = [[SsehSettingViewController alloc]init];
            TAB_PUSH(self, settingVC, YES);
        }break;
        default:
            break;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 0.18 * F_DEVICE_H;
    }else{
        return 44.0f;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.001;
    }
    return 30.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 0.001;
    }
    return 20.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }
    UIView *backVew = [[UIView alloc]initWithFrame:CGRectMake(0, 0, F_DEVICE_W, 30)];
    backVew.backgroundColor = [UIColor appColor];
    return backVew;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }
    UIView *backVew = [[UIView alloc]initWithFrame:CGRectMake(0, 0, F_DEVICE_W, 20)];
    backVew.backgroundColor = [UIColor appColor];
    return backVew;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
